from base64 import encode
import requests
from lxml import etree

class HaGongDa:
    def get_links(self):
        url = "http://news.hit.edu.cn/"
        headers = {
            'host' : "news.hit.edu.cn",
            'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",

        }
        response = requests.get(url)
        response.encoding = 'utf-8'#解决获取网页乱码的问题 
        rsp = response.text
        element = etree.HTML(rsp)
        links = element.xpath("//div[@class='row-a1']/div/div[2]/div[2]/ul/li/p/a/@href")
        print(links)
        return links #返回值

    def get_news(self):
        links = self.get_links()
        print(links)
        for link in  links:
            url = "http://news.hit.edu.cn/" + link
            print(url)
            response = requests.get(url)
            response.encoding = "utf-8"
            print(response.text)
            

if __name__ == '__main__':
    hgd = HaGongDa()
    #hgd.get_links()
    hgd.get_news()
